/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import entities.Restaurant;
import entities.Spot;
import entities.User;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;

/**
 *
 * @author HP
 */
@ManagedBean(name = "newspotbean")
@RequestScoped

public class NewSpotBean {
    
    
    @PersistenceContext 
    private EntityManager em;

    private String restaurantName;
    private String restaurantLocation;
    private String restaurantPic;
    private String spotName;
    private String spotPic;
    private String note;
    private Integer rid;
    private Restaurant rest=null;
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

 

    
   
    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public String getRestaurantLocation() {
        return restaurantLocation;
    }

    public void setRestaurantLocation(String restaurantLocation) {
        this.restaurantLocation = restaurantLocation;
    }

    public String getRestaurantName() {
        return restaurantName;
    }

    public void setRestaurantName(String restaurantName) {
        this.restaurantName = restaurantName;
    }

    public String getRestaurantPic() {
        return restaurantPic;
    }

    public void setRestaurantPic(String restaurantPic) {
        this.restaurantPic = restaurantPic;
    }

    public String getSpotName() {
        return spotName;
    }

    public void setSpotName(String spotName) {
        this.spotName = spotName;
    }

    public String getSpotPic() {
        return spotPic;
    }

    public void setSpotPic(String spotPic) {
        this.spotPic = spotPic;
    }
    
    public Integer getRid() {
        return rid;
    }

    public void setRid(Integer rid) {
        this.rid = rid;
    }
    
    private List<Restaurant> getRestaurant() {
        try {
                List<Restaurant> rst = (List<Restaurant>) em.createNamedQuery("Restaurant.findByName").setParameter("name", restaurantName).getResultList();
                return rst;
            } catch (NoResultException nre) {
                return null;
            }
    }
    private void setRestaurant(Restaurant a){
        //rest = new Restaurant();
        rest = a;
    } 
    
    public void CreateSpot(){
        
        FacesContext context = FacesContext.getCurrentInstance();
        Spot spot= new Spot();
        spot.setName(spotName);
        spot.setPic(spotPic);
        spot.setNote(note);
        spot.setRid(rest);
        spot.setUsername(user);
         try {
            
            em.persist(spot);
            

        } catch (Exception e) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Error creating Spot!",
                    "Unexpected error when creating this spot.  Please contact the system Administrator");
            context.addMessage(null, message);
            Logger.getAnonymousLogger().log(Level.SEVERE,
                    "Unable to create new spot",
                    e);

        }
    }
}
